package com.example.idlefish.HomeAndSearch

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.idlefish.PublicModule.ApplicationConst
import com.example.idlefish.PublicModule.ApplicationConst.NetRequestForGoodContext
import com.example.idlefish.R
import okhttp3.Callback
import okhttp3.Response
import org.json.JSONObject
import java.io.IOException

class TypeAdapter (private val items: List<one_goods_data>, // 数据源
private val listener: (childPosition: Int, data: Int) -> Unit
) : RecyclerView.Adapter<TypeAdapter.TypeViewHolder>() {
    class TypeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val goods_id : TextView = itemView.findViewById(R.id.goods_id)
        val type_one : ImageView = itemView.findViewById(R.id.type_one)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TypeAdapter.TypeViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.search_type, parent, false)
        val viewHolder = TypeViewHolder(view)
        return viewHolder
    }

    override fun onBindViewHolder(holder: TypeViewHolder, position: Int) {
        val item = items[position]
        holder.goods_id.text = item.sale_name


        NetRequestForGoodContext(item.goods_id.toLong(),  object : Callback {
            // 实现 onFailure 方法

            override fun onFailure(call: okhttp3.Call, e: IOException) {
                println("请求失败: ${e.message}")
            }

            override fun onResponse(call: okhttp3.Call, response: Response) {
                response.use {
                    if (!response.isSuccessful) {
                        // 如果响应失败，输出状态码
                        println("请求失败，状态码: ${response.code}")
                        return
                    }

                    // 输出响应体内容
                    val responseBody = response.body?.string()
                    println("请求成功，响应内容: $responseBody")
                    if (responseBody != null) {
                        try {
                            // 将响应体字符串解析为 JSON 对象
                            val jsonObject = JSONObject(responseBody)

                            val queryGoodResponse = jsonObject.getJSONObject("queryGoodResponse")
                            val picEncodingList = queryGoodResponse.getJSONArray("picEncodeingList")
                            println(picEncodingList)
                            val bitmap = ApplicationConst.decodeBase64ToImage(holder.itemView.context,picEncodingList.get(0).toString())
                            if (bitmap != null) {
                                (holder.itemView.context as Activity).runOnUiThread {
                                    holder.type_one.setImageBitmap(bitmap)
                                }
                            }
                        } catch (e: Exception) {
                            println("解析 JSON 失败: ${e.message}")
                        }
                    }
                }
            }
        })
        holder.itemView.setOnClickListener {
            val id = item.goods_id
            listener(position, id) // 通知外部点击事件
        }
    }

    override fun getItemCount(): Int = items.size
}